package com.peakmain.systemmoudle.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.peakmain.baselibrary.recyclerview.listener.OnItemClickListener;
import com.peakmain.sysemmoudle.R;
import com.peakmain.sysemmoudle.R2;
import com.peakmain.systemmoudle.adapter.SystemListAdapter;
import com.peakmain.systemmoudle.base.BaseSystemFragment;
import com.peakmain.systemmoudle.contract.SystemListContract;
import com.peakmain.systemmoudle.presenter.SystemListPresenter;
import com.peakmain.wanandroid.netmodule.bean.system.SystemListBean;
import com.peakmain.wanandroid.netmodule.constant.Constant;
import com.peakmain.wanandroid.netmodule.utils.XRefreshUtils;

import butterknife.BindView;

/**
 * author: peakmain
 * createdata：2019/7/16
 * mail: 2726449200@qq.com
 * desiption:
 */
public class SystemListFragment extends BaseSystemFragment<SystemListPresenter> implements SystemListContract.View {
    @BindView(R2.id.xrefresh_view)
    protected XRefreshView mXrefreshView;
    @BindView(R2.id.recycler_view)
    protected RecyclerView mRecyclerView;
    private int pageNum=0;
    private int totalPage=0;
    private int id;
    private SystemListAdapter mSystemAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_system_list;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initView(View view) {
        Bundle bundle = getArguments();
        id = bundle.getInt("id");

        initRecyclerView();
    }

    private void initRecyclerView() {
        XRefreshUtils.getInstance().initXRefresh(mXrefreshView,getContext());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        mXrefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                getList(true, false);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getList(false, true);
            }
        });
    }

    private void getList(boolean isPullDown, boolean isSilence) {
        if (isPullDown) {
            pageNum = 0;
        }
        if (isSilence) {
            if (pageNum+1 > totalPage) {
                return;
            }
        }
        mPresenter.getSystemList(pageNum, id, isPullDown, isSilence);
    }

    @Override
    public void showSystemListSuccess(SystemListBean data, boolean isPullDown, boolean isSilence) {
        mXrefreshView.stopRefresh();
        mXrefreshView.stopLoadMore();
        setData(data);
    }
    private void setData(final SystemListBean result) {
        pageNum = result.getCurPage();
        totalPage = result.getPageCount();
        mSystemAdapter=new SystemListAdapter(getContext(),result.getDatas());
        mRecyclerView.setAdapter(mSystemAdapter);
        mSystemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                startBrowserActivity(getContext(), Constant.MODE_DEFAULT,result.getDatas().get(position).getLink());
            }
        });
        if (pageNum > totalPage) {
            mXrefreshView.setLoadComplete(true);
        }

    }
    @Override
    public void onResume() {
        if (mXrefreshView != null) {
            mXrefreshView.startRefresh();
        }
        super.onResume();
    }
    public static Fragment getInstance(int id) {
        SystemListFragment fragment = new SystemListFragment();

        Bundle bundle = new Bundle();
        bundle.putInt("id",id);
        fragment.setArguments(bundle);
        return fragment;
    }
}
